<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="模版名称" prop="name">
                <el-input v-model="queryParams.name" placeholder="请输入模版名称" clearable @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item label="是否可用" prop="status">
                <el-select v-model="queryParams.status" placeholder="请选择是否可用" clearable>
                    <el-option v-for="dict in dict.type.sys_normal_disable" :key="dict.value" :label="dict.label" :value="dict.value"/>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <TableCard>
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['oa:officeDocRedheadtpl:add']">新增</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['oa:officeDocRedheadtpl:edit']">修改</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['oa:officeDocRedheadtpl:remove']">删除</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['oa:officeDocRedheadtpl:export']">导出</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>

            <el-table  :border="Global.tableShowBorder" v-loading="loading" :data="officeDocRedheadtplList" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column label="模版名称" align="center" prop="name" />
                <el-table-column label="公章名称" align="center" prop="sealName" width="150"/>
                <el-table-column label="公司名称（公文使用）" align="center" prop="company"/>
                <el-table-column label="背景" align="center" prop="transparent" width="150">
                    <template slot-scope="scope">
                        <span v-if="scope.row.transparent=='0'" style="color: #f56c6c;">白色背景</span>
                        <span v-if="scope.row.transparent=='1'" style="color: #4AB7BD;">透明背景</span>
                    </template>
                </el-table-column>
                <el-table-column label="图片" align="center" prop="img" width="100">
                    <template slot-scope="scope">
                        <image-preview v-if="!util.isEmpty(scope.row.img)" :src="scope.row.img" :width="50" :height="50"/>
                    </template>
                </el-table-column>
                <el-table-column label="模版" align="center" prop="tpl" width="300">
                    <template slot-scope="scope">
                        <div v-if="1==1">
                            <div style="color: red;font-weight: 900;text-align: center;">{{scope.row.redTitle}}</div>
                            <div style="color: #000000;text-align: center;font-size: 12px;">锦翔信息科技企字（2023）第【1】号</div>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="是否可用" align="center" prop="status" width="150">
                    <template slot-scope="scope">
                        <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
                    <template slot-scope="scope">
                        <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['oa:officeDocRedheadtpl:edit']">修改</el-button>
                        <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['oa:officeDocRedheadtpl:remove']">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>
        </TableCard>

        <!-- 添加或修改套红模版对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                <el-form-item label="模版名称" prop="name">
                    <el-input v-model="form.name" placeholder="请输入模版名称" />
                </el-form-item>
                <el-form-item label="红头标题" prop="redTitle">
                    <el-input v-model="form.redTitle" placeholder="请输入红头标题" />
                </el-form-item>
                <el-form-item label="选择公章" prop="sealId">
                    <el-select v-model="form.sealId" placeholder="请选择使用公章" clearable @change="changeSeal">
                        <el-option v-for="seal in sealList" :key="seal.id" :label="seal.name" :value="seal.id"/>
                    </el-select>
                </el-form-item>
                <el-form-item label="图片" prop="img">
                    <image-upload v-model="form.img" disabled/>
                </el-form-item>
                <el-form-item label="公司名称" prop="company">
                    <el-input v-model="form.company" placeholder="请输入公司名称" disabled/>
                </el-form-item>
                <el-form-item label="背景">
                    <el-select v-model="form.transparent" placeholder="请选择背景类型" disabled clearable>
                        <el-option key="0" label="白色背景" value="0"/>
                        <el-option key="1" label="透明背景" value="1"/>
                    </el-select>
                </el-form-item>
                <el-form-item label="是否可用">
                    <el-radio-group v-model="form.status">
                        <el-radio
                            v-for="dict in dict.type.sys_normal_disable"
                            :key="dict.value"
                            :label="dict.value"
                        >{{dict.label}}</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listOfficeDocRedheadtpl, getOfficeDocRedheadtpl, delOfficeDocRedheadtpl, addOfficeDocRedheadtpl, updateOfficeDocRedheadtpl } from "@/api/oa/officeDocRedheadtpl";
import {listSealAll} from "@/api/oa/seal"
export default {
    name: "OfficeDocRedheadtpl",
    dicts: ['sys_normal_disable'],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 套红模版表格数据
            officeDocRedheadtplList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 20,
                orderByColumn:"createTime",
                isAsc:"descending",
                name: null,
                tpl: null,
                status: null
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
            },
            sealList:[]
        };
    },
    created() {
        this.getList();
        this.selectSeal();
    },
    methods: {
        selectSeal(){
            listSealAll().then(res=>{
                if(200==res.code){
                    this.sealList = res.data;
                }
            })
        },
        changeSeal(seal){
            if(seal){
                console.log(seal);
                this.form.img = this.sealList[seal-1].img;
                this.form.company = this.sealList[seal-1].company;
                this.form.transparent = this.sealList[seal-1].transparent;
            }else{
                console.log(seal);
                this.form.img = "";
                this.form.company = "";
                this.form.transparent = "";
            }

        },
        /** 查询套红模版列表 */
        getList() {
            this.loading = true;
            listOfficeDocRedheadtpl(this.queryParams).then(response => {
                this.officeDocRedheadtplList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                name: null,
                tpl: null,
                createTime: null,
                status: "0"
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length!==1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加套红模版";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids
            getOfficeDocRedheadtpl(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改套红模版";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateOfficeDocRedheadtpl(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addOfficeDocRedheadtpl(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除套红模版编号为"' + ids + '"的数据项？').then(function() {
                return delOfficeDocRedheadtpl(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => {});
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('oa/officeDocRedheadtpl/export', {
                ...this.queryParams
            }, `officeDocRedheadtpl_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>
